//
//  BorrowViewController.m
//  TenThousand
//
//  Created by 我的 on 16/6/24.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "BorrowViewController.h"
#import "BorrowTableViewCell.h"
#import "ReimbursementTableViewController.h"
#import "BorrowModel.h"

@interface BorrowViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    BOOL _isLastPage;
    NSInteger _currentPage;
}
@property (nonatomic, strong) NSMutableArray *borrArr;
@end

@implementation BorrowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:YES];
    } right:nil rightClick:nil];
    
    [self.view addSubview:self.borrowTableView];


}

//存储解析的数据
-(NSMutableArray *)borrArr{
    if (!_borrArr) {
        _borrArr = [NSMutableArray array];
    }
    return _borrArr;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSString *method;
    if (self.index == 0) {
        //全部借款
        method = @"Borrow.GetBorrowListALL";
    } else if(self.index == 1){
        //招标中的借款
        method = @"Borrow.GetBorrowListbidding";
    }else if (self.index == 2){
        //还款中的借款
        method = @"Borrow.GetBorrowListpayment";
    }else{
        method = @"Borrow.GetBorrowListFinance";//已还完
    }
    [self getBorrowList:method WithPage:1];
    
    [self refreshPageWithMethod:method];
}

- (void)getBorrowList:(NSString *)method WithPage:(NSInteger)page{
    
    NSString *loginId = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginId"];
    NSString *index = [NSString stringWithFormat:@"%ld",page];
    NSDictionary *dict = @{
                           @"PageIndex":index,
                           @"LoginId":loginId
                           };
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:method withParaments:dict withSuccessBlock:^(NSDictionary *object) {
        
        NSLog(@"%@",object);
        [self.borrArr removeAllObjects];
        
        if (self.index == 2) {
            NSArray *arr = [object[@"Data"] firstObject][@"List"];
            for (NSDictionary *dic in arr) {
                BorrowModel *model = [BorrowModel mj_objectWithKeyValues:dic];
                [self.borrArr addObject:model];
            }

        }else{
            
            NSArray *arr = [object[@"Data"] firstObject][@"ItemList"];
            for (NSDictionary *dic in arr) {
                BorrowModel *model = [BorrowModel mj_objectWithKeyValues:dic];
                [self.borrArr addObject:model];
            }

        
               }
        [_borrowTableView reloadData];
        
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@", error);
    } progress:^(float progress) {
        
    }];
}

- (UITableView *)borrowTableView{
    
    if (!_borrowTableView) {
        _borrowTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height-64-40) style:UITableViewStyleGrouped];
        _borrowTableView.delegate = self;
        _borrowTableView.dataSource = self;
        _borrowTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _borrowTableView.showsVerticalScrollIndicator = NO;
    }
    
    return _borrowTableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.borrArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    BorrowTableViewCell *borrowCell = [tableView dequeueReusableCellWithIdentifier:@"BORROWTABLEVIEWCELL"];
    if (borrowCell  == nil) {
        borrowCell = [[[NSBundle mainBundle] loadNibNamed:@"BorrowTableViewCell" owner:nil options:nil] lastObject];
    }
    
    borrowCell.selectionStyle = UITableViewCellSelectionStyleNone;
    //cell添加阴影
    borrowCell.layer.shadowOffset =CGSizeMake(0, 1);
    borrowCell.layer.shadowColor = [UIColor grayColor].CGColor;
    borrowCell.layer.shadowRadius = 1;
    borrowCell.layer.shadowOpacity = .5f;
    
    
    if (self.borrArr.count) {
        BorrowModel *model = self.borrArr[indexPath.section];

        if (self.index == 2) {
            [borrowCell setDataAndModel:model];
        }else{
        
            [borrowCell setDataWithModel:model];
        }
    }
    
    return borrowCell;

  
}

//MJ刷新
-(void)refreshPageWithMethod:(NSString *)method
{
    
    //下拉刷新
    self.borrowTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^
                                {
                                    
                                   [self getBorrowList:method WithPage:1];
                                    
                                    [self.borrowTableView.mj_header endRefreshing];
                                }];
    //上拉加载
    self.borrowTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        
        
        if (_isLastPage) {
            [self.borrowTableView.mj_footer endRefreshing];
            return;
        }
        
        [self getBorrowList:method WithPage:_currentPage++];
        
        [self.borrowTableView.mj_footer endRefreshing];
    }];
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  
    if (section == 0) {
        return 0.001;
    }
       
    return 8;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 135;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.index == 2) {
        
        //跳转到我的还款计划列表
        ReimbursementTableViewController *reimbView = [[ReimbursementTableViewController alloc] init];
        reimbView.index = 1;
        if (self.borrArr.count) {
            reimbView.boModel = self.borrArr[indexPath.row];
            [self.navigationController pushViewController:reimbView animated:YES];
        }
       
    }
    
}





@end
